/**
 * 
 */
package br.com.games.daedal.imp;

import java.util.Arrays;
import java.util.Random;

import br.com.games.daedal.Box;
import br.com.games.daedal.DaedalTeam;
import br.com.games.daedal.Vertex;
import br.com.games.hefesto.Team;

/**
 * Representa um unidade fechada no jogo
 * 
 * @since 2013
 * @author <a href="mailto:fernando.schindler@gmail.com">Fernando Schindler</a>
 */
public class DaedalBox implements Box {

	/**
	 * Criar uma nova caixa
	 * @param verts
	 */
	public DaedalBox(DaedalVertex[] verts, boolean isClassicMode) {
		this.value = isClassicMode ? 1 : (RANDOM.nextInt(48)+2);
		this.team = DaedalTeam.NONE;
		this.vertex = verts;
		
		for (DaedalVertex v : verts){
			v.addOwener(this);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see br.com.games.daedal.Box#getOwner()
	 */
	@Override
	public Team getOwner() {
		return this.team;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see br.com.games.daedal.Box#getValue()
	 */
	@Override
	public Short getValue() {
		return this.value.shortValue();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see br.com.games.daedal.Box#isClosed()
	 */
	@Override
	public Boolean isClosed() {
		Boolean result = true;

		for (Vertex v : this.vertex) {
			if (!v.isActive()) {
				result = false;
				break;
			}
		}

		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see br.com.games.daedal.Box#setOwener(br.com.games.hefesto.Team)
	 */
	@Override
	public void setOwener(Team team) {
		this.team = team;
	}
	
	/*
	 * (non-Javadoc)
	 * @see br.com.games.daedal.Box#getVertexes()
	 */
	@Override
	public Vertex[] getVertexes() {
		return vertex;
	}
	
	@Override
	public String toString() {
		return "{"+Arrays.toString(vertex)+"}";
	}
	
	/*
	 * (non-Javadoc)
	 * @see br.com.games.daedal.Box#reset()
	 */
	@Override
	public void reset() {
		this.value = this.value == 1 ? 1 : RANDOM.nextInt(48)+2;		
	}

	/**
	 * Ajustar o valor manualmente
	 * @param value
	 */
	public void setValue(Integer value) {
		this.value = value;
	}
	
	/**
	 * Campos privados
	 */
	private Team team;
	private Vertex [] vertex;
	private Integer value;
	private static final Random RANDOM = new Random();
}
